[ Vue筆記 ] Vue使用setInterval


Posted by Akira on 2022-02-20

前言

最近在寫番茄鐘的Side project,在使用 setInterval 做計時器時,遇到了小小問題。
這邊紀錄一下解法。

範例

  data() {
    return {
      time: null,
    };
  },
  methods: {
    timer() {
      this.time = setInterval(
        function () {
        // 想重複的動作
        }.bind(this),
        200
      );
    },
    setTime() {
      this.timer();
    },
    stopTime() {
      if (this.time) {
        clearInterval(this.time);
        this.time = null;
      }
    },
}









Related Posts

Twitch API壞掉惹,害我debug 超久QQ

Twitch API壞掉惹,害我debug 超久QQ

[Day 02] - Vault 的啟動及 Unseal 概念

[Day 02] - Vault 的啟動及 Unseal 概念

一起用 Google Calendar 與 D3.js 進行年終回顧吧!

一起用 Google Calendar 與 D3.js 進行年終回顧吧!


Comments